package com.best.weiyi.fragment;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.best.weiyi.R;
import com.best.weiyi.entity.Music;
import com.best.weiyi.main.MainActivity;
import com.best.weiyi.main.Way;
import com.best.weiyi.util.MusicUtil;

public class MusicList extends Fragment{
	MainActivity m = new MainActivity();
	
	//显示本地音乐
	ListView musicList;
	LinearLayout sequence;
	
	ImageView iv_shunxu;
	TextView tv_shunxu;
	
	public List<Music> music = new ArrayList<Music>();
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View v = inflater.inflate(R.layout.musiclist_fragment, container,false);
		
		/**
		 * 显示本地音乐
		 */
		musicList = (ListView) v.findViewById(R.id.musicList);
		Way.music = MusicUtil.getMusicData(getActivity());
		//System.out.println(Way.music.size());
		List<String> ls = new ArrayList<String>(); 
		for(Music ms :Way.music){
			ls.add(ms.getName());
		}
		//适配器   为什么我自己做的适配不能用  好烦好烦！！！！！
		musicList.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,ls));
		//musicList.setAdapter(new MusicListAdapter(getActivity(), music));
		//点击播放本地音乐
		musicList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				m.playAndPause.setImageDrawable(getResources().getDrawable(R.drawable.alarm_pause_btn));
				Way.index = arg2;
				String path = Way.music.get(Way.index).getUrl();
				m.way.play(path);  
				int max = (int) Way.music.get(arg2).getTime();
				m.sb.setMax(max);
				m.pubProgress = 0;
				m.sb.setProgress(m.pubProgress);
				m.sb.setMax((int) Way.music.get(Way.index).getTime());
				m.isPause = true;
				//进度条
				m.hanlder.post(m.run);
			}
		});
		iv_shunxu = (ImageView) v.findViewById(R.id.iv_shunxu);
		tv_shunxu = (TextView) v.findViewById(R.id.tv_shunxu);
		

		sequence = (LinearLayout) v.findViewById(R.id.sequence);
		sequence.setOnClickListener(new OnClickListener() {
			
			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {
				
				PopupMenu pop = new PopupMenu(getActivity(),v);
				pop.getMenuInflater().inflate(R.menu.popupmenu, pop.getMenu());
				pop.show();
				pop.setOnMenuItemClickListener(new OnMenuItemClickListener() {
					//获取歌曲的总长度
					int time = (int) m.way.music.get(m.way.index).getTime();
					
					/**此处为点击选择播放的顺序
					 * *可以选择列表循环、单曲循环、随机播放、顺序播放
					 * 后期优化代码时可以将播放的顺序写成方法  放到way类当中
					 * */
					
					@SuppressLint("ShowToast")
					@Override
					public boolean onMenuItemClick(MenuItem menu) {
						// TODO Auto-generated method stub
						switch (menu.getItemId()) {
						
						//列表循环
						case R.id.allCirculate:
							m.sb.setMax(time);
							iv_shunxu.setImageDrawable(getResources().getDrawable(R.drawable.ic_player_mode_all_default));
							tv_shunxu.setText("列表循环");
							m.way.playMode = 0x02;
							Toast.makeText(getActivity(), "列表循环", Toast.LENGTH_LONG).show();
							
							break;
						//单曲循环
						case R.id.oneCirculate:
							m.sb.setMax(time);
							iv_shunxu.setImageDrawable(getResources().getDrawable(R.drawable.ic_player_mode_single_default));
							tv_shunxu.setText("单曲循环");
							m.way.playMode = 0x01;
							Toast.makeText(getActivity(), "单曲循环", Toast.LENGTH_LONG).show();
							
							break;
						//顺序播放
						case R.id.sequencePlay:
							m.sb.setMax(time); 
							iv_shunxu.setImageDrawable(getResources().getDrawable(R.drawable.ic_player_mode_sequence_default));
							tv_shunxu.setText("顺序播放");
							m.way.playMode = 0x04;
							Toast.makeText(getActivity(), "顺序播放", Toast.LENGTH_LONG).show();
							
							break;
							
						//随机播放
						case R.id.randomPlay:
							m.sb.setMax(time);
							iv_shunxu.setImageDrawable(getResources().getDrawable(R.drawable.ic_player_mode_random_default));
							tv_shunxu.setText("随机播放");
							m.way.playMode = 0x03;
							Toast.makeText(getActivity(), "随机播放", Toast.LENGTH_LONG).show();
							
							break;
							
						default:
							break;
						}
						return false;
					}
				});	
			}
		});
		return v;
	}	
	
}
